#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include<QLabel>
#include "playscene.h"
#include <QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(600,888);

    //设置图标
    this->setWindowIcon(QPixmap(":/new/prefix1/res/Coin0001.png"));

    //设c置标题
    this->setWindowTitle("选择游戏关卡");

    //创建菜单栏
    QMenuBar *bar= menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu =bar->addMenu("开始");

    //创建退出的菜单项
    QAction *quitAction=startMenu->addAction("退出");

    //点击退出
    connect(quitAction,&QAction::triggered,[=]{
        this->close();
    });

    QSound * backSound = new QSound(":/new/prefix1/res/BackButtonSound.wav");

    //返回按钮

    MyPushButton *backBtn = new MyPushButton(":/new/prefix1/res/BackButton.png",":/new/prefix1/res/BackButtonSelected.png");
    backBtn->setParent(this);
//    backBtn->close();    backBtn->resize(backBtn->width()*2,backBtn->height()*2);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=]{
        //qDebug()<<"点了返回按钮";
        backSound->play();
        //告诉主场景，我返回了，主场景监听ChooseLevelScene的返回按钮
        QTimer::singleShot(400,this,[=](){
            emit this->ChooseSceneBack();

        });

    });
    //选择关卡音效
    QSound *chooseSound = new QSound(":/new/prefix1/res/TapButtonSound.wav");

    //创建选择关卡的按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton * menuBtn = new MyPushButton(":/new/prefix1/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(i%4*120+60,i/4*120+200);

        //监听每个按钮的点击事件
        connect(menuBtn,&MyPushButton::clicked,[=](){
//            QString str = QString("选择的是第 %1 关").arg( i + 1 );
//            qDebug()<<str;
            chooseSound->play();

            //进入到游戏场景中
            this->hide();//将选关场景隐藏掉
            play = new PlayScene (i+1);//创建
            play->setGeometry(this->geometry());
            play->show();
            connect(play,&PlayScene::ChooseSceneBack,[=](){
                this->setGeometry(play->geometry());
                delete play;
                this->show();
                play = NULL;
            });
        });

        QLabel * label =new QLabel;
        QFont font;
        font.setFamily("华文新魏");
        font.setPixelSize(30);
        label->setFont(font);
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(i%4*120+60,i/4*120+200);

        //设置label上的文字对齐方式 水平居中和垂直居中
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

        //设置让鼠标进行穿透 51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/new/prefix1/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //画背景上的图标
    pix.load(":/new/prefix1/res/Title.png");
    pix=pix.scaled(pix.width()*1.6,pix.height()*1.2);
    painter.drawPixmap((this->width()-pix.width())*0.5,50,pix);
}
